// url: https://leetcode.cn/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150
package interview_classic.array_or_str;

import java.util.Arrays;

public class Solution189 {
    // way1
    public void rotate1(int[] nums, int k) {
        int[] temp = Arrays.copyOf(nums, nums.length);
        k %= nums.length;

        for(int i = 0; i < nums.length; ++i) {
            nums[(i + k) % nums.length] = temp[i];
        }

    }

    // way2 看不懂...
    public void rotate(int[] nums, int k) {
        k %= nums.length;
        if (k != 0) {
            for (int i = 0; i < nums.length / k; ++i) {

            }
        }
    }

    // way3 reverse
    public void rotate3(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }

    private void reverse(int[] nums, int l, int r) {
        if (r > l) {
            int count = (r - l + 1) / 2;
            for(int i = 0; i < count; ++i) {
                swap(nums, l + i, r - i);
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
